package omm.domain;

import org.joda.time.DateTime;

public class Quotation extends PersistentAttributes implements Comparable<Quotation>{
	private Integer subid;
	private Order order;
	private Risk risk;
	private Company company;
	private Double entityCommission;
	private Double aonCommission;
	private Double percentageCommision;
	private Double prime;
	private State state;
	private String modifierUser;
	private String creatorUser;
	private String specialistUser;
	private DateTime creationDate;
	private DateTime modificationDate;
	private DateTime resolutionDate;
	private Double prize;
	private Double actualPrize;
	private LostReason lostReason;
	
	public Quotation(Integer subid, Order order, Risk risk, Company company,
			Double entityComission, Double aonCommission,Double percentageCommision, 
			Double prime, State state,
			String modifierUser, String creatorUser, String specialistUser,
			DateTime creationDate, DateTime modificationDate, DateTime resolutionDate,
			Double prize, Double actualPrize, LostReason lostReason){
		this.subid = subid;
		this.order = order;
		this.risk = risk;
		this.company = company;
		this.entityCommission = entityComission;
		this.aonCommission = aonCommission;
		this.percentageCommision = percentageCommision;
		this.prime = prime;
		this.state = state;
		this.modifierUser = modifierUser;
		this.creatorUser = creatorUser;
		this.specialistUser = specialistUser;
		this.creationDate = creationDate;
		this.modificationDate = modificationDate;
		this.resolutionDate = resolutionDate;
		this.prize = prize;
		this.actualPrize = actualPrize;
		this.lostReason = lostReason;
	}
	
	public void setModificationDate(DateTime modificationDate) {
		this.modificationDate = modificationDate;
	}

	public State getState(){
		return state;
	}
	
	public Double getAonCommision(){
		return aonCommission;
	}
	
	public DateTime getModificationDate(){
		return modificationDate;
	}
	
	public Risk getRisk(){
		return risk;
	} 
	
	public Order getOrder(){
		return order;
	}
	
	public enum State {
		NULL(0), QUOTED(1), NO_INTEREST(2), PARTICIPATED(4), WON(5), LOST(6);
		
		private int code;
			
		State(int code){
			this.code = code;
		}

		int getCode() {
			return code;
		}
		
		public static State fromCode(Integer code) {
			if (code != null) {
				for (State b : State.values()) {
					if (code == b.getCode()) {
						return b;
					}
				}
			}
			return null;
		}
	}

	public enum LostReason {
		LOSTR_NO_INFO(1), LOSTR_PRICE(2), LOSTR_PRODUCER(3),
		LOSTR_ACT_SIN(4), LOSTR_PAUTA(5), LOSTR_TIME(6),
		LOSTR_CARTERA(7), LOSTR_COST(8), LOSTR_COBERT(9),
		LOSTR_OTHERS(10);
		
		private int code;
			
		LostReason(int code){
			this.code = code;
		}

		int getCode() {
			return code;
		}
		
		public static LostReason fromCode(Integer code) {
			if (code != null) {
				for (LostReason b : LostReason.values()) {
					if (code == b.getCode()) {
						return b;
					}
				}
			}
			return null;
		}
	}
	
	@Override
	public String toString(){
		String output = "ID: "+ (this.order  != null? this.order.getId(): " ")
		+"\tEMPRESA: "+ (this.order  != null? this.order.getName(): " ")
		+"\tRiskID"+ (this.risk  != null? risk.getId() : " ")
		+"\tSUBID" + this.subid
		+"\t ESTADO: "+ this.state
		+"\t premio: "+this.prize+
		"\t commision: "+this.getAonCommision();
		return output;
	}

	@Override
	public int compareTo(Quotation o) {
		
		if (o.getOrder() == null){
			int a = 1;
		}else if(o.getRisk() == null){
			int a = 1;
		}else if(o.getSubid() == null){
			int a = 1;
		}else if(o.getState() == null){
			int a = 1;
		}else if(o.getPrize() == null){
			int a = 1;
		}
	
		int result = order.compareTo(o.getOrder());
		if(result == 0 ) {
			result = risk.compareTo(o.getRisk());
		}
		if(result == 0){
			result = subid.compareTo(o.getSubid());
		}
		if(result == 0){
			result = o.getState().compareTo(state);
		}
		if(result == 0){
			result = prize.compareTo(o.getPrize());
		}
		return result;
	}

	public Double getPrize() {
		return prize;
	}

	public Integer getSubid() {
		return subid;
	}
	
}
